# 
# Description:
# This script constructs Tables B1-B18
# for Appendix B
#
# Requires: pres_all_terror.dta, county_balance.RDS, terror_pres_wide.dta, house_all_terror.dta, terror_house_wide, senate_all_terror.dta, terror_senate_wide
#
# Author: Sean Nossek
#
# Date: August 04, 2020

# Load Packages

if (!require("pacman")) install.packages("pacman")

#lfe version manage
packageurl <- "https://cran.r-project.org/src/contrib/Archive/lfe/lfe_2.8.tar.gz"
install.packages(packageurl, repos=NULL, type="source")
library(lfe)

pacman::p_load(readxl,
               tidyverse,
               readstata13,
               stargazer,
               margins,
               lubridate,
               gridExtra)

# set wd

setwd("C:/Users/Sean/Dropbox/Eran/Terrorism_Left/Paper/Revisions/Revised Paper/Replication/Paper Data")


# load panel presidential data

pres_all <- read.dta13("pres_all_terror.dta") %>% 
  mutate(days_till_election_last = ifelse(is.na(days_till_election_last), 0, days_till_election_last),
         two_party_share = rep_perc_vote + dem_perc_vote, rep_tp_share = (rep_perc_vote / two_party_share) * 100,
         county_fips = str_pad(county_fips, 3, pad = "0"), fips = paste0(state_fips, county_fips))

# code incumbency

pres_all$incumbent[pres_all$election_year == 1972] <- "R"
pres_all$incumbent[pres_all$election_year == 1976] <- "R"
pres_all$incumbent[pres_all$election_year == 1980] <- "D"
pres_all$incumbent[pres_all$election_year == 1984] <- "R"
pres_all$incumbent[pres_all$election_year == 1988] <- "R"
pres_all$incumbent[pres_all$election_year == 1992] <- "R"
pres_all$incumbent[pres_all$election_year == 1996] <- "D"
pres_all$incumbent[pres_all$election_year == 2000] <- "D"
pres_all$incumbent[pres_all$election_year == 2004] <- "R"
pres_all$incumbent[pres_all$election_year == 2008] <- "R"
pres_all$incumbent[pres_all$election_year == 2012] <- "D"
pres_all$incumbent[pres_all$election_year == 2016] <- "D"

pres_all$incumbent_rep <- as.numeric(pres_all$incumbent == "R")

pres_all$trend <- as.numeric(pres_all$election_year - min(pres_all$election_year))

pres_all$GeoFIPS <- str_pad(pres_all$fips,5,side = "left", "0")

# load and merge controls

controls <- readRDS("county_balance.RDS") %>%
  dplyr::select(year, GeoFIPS, l_jobs_per_cap, l_earnings_per_cap, l_retirement_per_cap,l_unemp_insurance_per_cap,l_murder_per_cap) %>% 
  rename(election_year = year)

pres_all <- merge(pres_all, controls, by = c("GeoFIPS","election_year"), all.x = TRUE)

# Add different attack windows

pres_all_counts <- read.dta13("terror_pres_wide.dta")

pres_all_counts$propvalue <- as.numeric(pres_all_counts$propvalue) / 1000000

pres_all_counts$nkill_270 <- pres_all_counts$nkill
pres_all_counts$nkill_180 <- pres_all_counts$nkill  
pres_all_counts$nkill_90 <- pres_all_counts$nkill

pres_all_counts$nkill_270[pres_all_counts$event_to_t2 > 270] <- 0
pres_all_counts$nkill_180[pres_all_counts$event_to_t2 > 180] <- 0
pres_all_counts$nkill_90[pres_all_counts$event_to_t2 > 90] <- 0

pres_all_counts$propvalue_270 <- pres_all_counts$propvalue
pres_all_counts$propvalue_180 <- pres_all_counts$propvalue  
pres_all_counts$propvalue_90 <- pres_all_counts$propvalue

pres_all_counts$propvalue_270[pres_all_counts$event_to_t2 > 270] <- 0
pres_all_counts$propvalue_180[pres_all_counts$event_to_t2 > 180] <- 0
pres_all_counts$propvalue_90[pres_all_counts$event_to_t2 > 90] <- 0

pres_all_counts <- pres_all_counts %>%
  filter(success == 1) %>%
  select(year_t2, FIPSCounty, success, event_to_t2, nkill_270, nkill_180, nkill_90, propvalue_270, propvalue_180, propvalue_90) %>%
  group_by(year_t2, FIPSCounty) %>%
  summarise(success_270 = sum(event_to_t2 <= 270),
            success_180 = sum(event_to_t2 <= 180),
            success_90 = sum(event_to_t2 <= 90),
            success_30 = sum(event_to_t2 <= 30),
            nkill_270 = sum(nkill_270), 
            nkill_180 = sum(nkill_180), 
            nkill_90 = sum(nkill_90),
            propvalue_270 = sum(propvalue_270),
            propvalue_180 = sum(propvalue_180),
            propvalue_90 = sum(propvalue_90)) %>%
  arrange(FIPSCounty, year_t2) %>%
  rename(fips = FIPSCounty, election_year = year_t2)

pres_all <- merge(pres_all, pres_all_counts, by = c("fips","election_year"), all.x = TRUE) %>%
  mutate(success_270 = ifelse(is.na(success_270), 0, success_270), 
         success_180 = ifelse(is.na(success_180), 0, success_180),
         success_90 = ifelse(is.na(success_90), 0, success_90),
         success_30 = ifelse(is.na(success_30), 0, success_30),
         nkill_270 = ifelse(is.na(nkill_270), 0, nkill_270),
         nkill_180 = ifelse(is.na(nkill_270), 0, nkill_180),
         nkill_90 = ifelse(is.na(nkill_270), 0, nkill_90))

pres_all$nkill_270[is.na(pres_all$nkill_270)] <- 0
pres_all$nkill_180[is.na(pres_all$nkill_180)] <- 0
pres_all$nkill_90[is.na(pres_all$nkill_90)] <- 0

pres_all$propvalue_270[is.na(pres_all$propvalue_270)] <- 0
pres_all$propvalue_180[is.na(pres_all$propvalue_180)] <- 0
pres_all$propvalue_90[is.na(pres_all$propvalue_90)] <- 0

# Load Congressional Elections

# House

house_all <- read.dta13("house_all_terror.dta") %>% 
  mutate(days_till_election_last = ifelse(is.na(days_till_election_last), 0, days_till_election_last),
         two_party_share = dem_perc_vote + rep_perc_vote, rep_tp_share = (rep_perc_vote / two_party_share) * 100)

house_all$trend <- as.numeric(house_all$election_year - min(house_all$election_year))

house_all$GeoFIPS <- str_pad(house_all$fips,5,side = "left", "0")

# merge controls

controls <- readRDS("county_balance.RDS") %>%
  dplyr::select(year, GeoFIPS, l_jobs_per_cap, l_earnings_per_cap, l_retirement_per_cap,l_unemp_insurance_per_cap,l_murder_per_cap) %>% 
  rename(election_year = year)

house_all <- merge(house_all, controls, by = c("GeoFIPS","election_year"), all.x = TRUE)

# Add different attack windows

house_all_counts <- read.dta13("terror_house_wide.dta") %>%
  filter(success == 1) %>% select(year_t2, FIPSCounty, success, event_to_t2) %>% 
  group_by(year_t2, FIPSCounty) %>%
  summarise(success_270 = sum(event_to_t2 <= 270),
            success_180 = sum(event_to_t2 <= 180),
            success_90 = sum(event_to_t2 <= 90),
            success_30 = sum(event_to_t2 <= 30)) %>%
  arrange(FIPSCounty, year_t2) %>%
  rename(fips = FIPSCounty, election_year = year_t2)

house_all <- merge(house_all, house_all_counts, by = c("fips","election_year"), all.x = TRUE) %>%
  mutate(success_270 = ifelse(is.na(success_270), 0, success_270), 
         success_180 = ifelse(is.na(success_180), 0, success_180),
         success_90 = ifelse(is.na(success_90), 0, success_90),
         success_30 = ifelse(is.na(success_30), 0, success_30))

# Senate

senate_all <- read.dta13("senate_all_terror.dta") %>% mutate(days_till_election_last = ifelse(is.na(days_till_election_last), 0, days_till_election_last), two_party_share = dem_perc_vote + rep_perc_vote, rep_tp_share = (rep_perc_vote / two_party_share) * 100)

senate_all$trend <- as.numeric(senate_all$election_year - min(senate_all$election_year))

senate_all$GeoFIPS <- str_pad(senate_all$fips,5,side = "left", "0")

# merge controls

senate_all <- merge(senate_all, controls, by = c("GeoFIPS","election_year"), all.x = TRUE)

# Add different attack windows

senate_all_counts <- read.dta13("terror_senate_wide.dta") %>% 
  filter(success == 1) %>%
  select(year_t2, FIPSCounty, success, event_to_t2) %>% group_by(year_t2, FIPSCounty) %>%
  summarise(success_270 = sum(event_to_t2 <= 270), 
            success_180 = sum(event_to_t2 <= 180), 
            success_90 = sum(event_to_t2 <= 90),
            success_30 = sum(event_to_t2 <= 30)) %>%
  arrange(FIPSCounty, year_t2) %>%
  rename(fips = FIPSCounty, election_year = year_t2)

senate_all <- merge(senate_all, senate_all_counts, by = c("fips","election_year"), all.x = TRUE) %>% 
  mutate(success_270 = ifelse(is.na(success_270), 0, success_270), 
         success_180 = ifelse(is.na(success_180), 0, success_180),
         success_90 = ifelse(is.na(success_90), 0, success_90),
         success_30 = ifelse(is.na(success_30), 0, success_30))

#######################################
##########        Tables      #########
#######################################



# Table B1
# year and county FEs, binary treatment

pres_all$success <- as.numeric(pres_all$success_count > 0)

mod1 <- felm(rep_tp_share ~ success | fips + election_year | 0 | fips, data = pres_all)
mod2 <- felm(rep_tp_share ~ success | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

#Table B2

mod1 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = pres_all)
mod2 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

#Table B3
pres_all$state_fips <- as.factor(pres_all$state_fips)
pres_all$election_year <- as.factor(pres_all$election_year)

mod1 <- felm(rep_tp_share ~ success_count | fips + election_year + state_fips:election_year | 0 | fips, data = pres_all)
mod2 <- felm(rep_tp_share ~ success_count | fips + election_year + state_fips:election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count | fips + election_year + state_fips:election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count | fips + election_year + state_fips:election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

#Table B4
pres_all$rep_perc_vote <- pres_all$rep_perc_vote * 100

mod1 <- felm(rep_perc_vote ~ success_count | fips + election_year | 0 | fips, data = pres_all)
mod2 <- felm(rep_perc_vote ~ success_count | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_perc_vote ~ success_count | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_perc_vote ~ success_count | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Percentage Vote", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

#Table B5
pres_sub <- pres_all %>% filter(incumbent_rep == 0)

mod1 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_sub)
mod2 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_sub %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_sub %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_sub %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months","1 month"))

#Table B6
pres_sub <- pres_all %>% filter(incumbent_rep == 1)

mod1 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_sub)
mod2 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_sub %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_sub %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_sub %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months","1 month"))

#Table B7 - Col 1. Cols 2-3 below.
mod1 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = pres_all)
mod2 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = house_all)
mod3 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = senate_all)

stargazer(mod1, mod2, mod3, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("Presidential Elections","House Elections","Senate Election"))

#Table B8 
mod1 <- felm(rep_tp_share ~ nkill_tot | fips + election_year | 0 | fips, data = pres_all)
mod2 <- felm(rep_tp_share ~ nkill_tot | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 270) %>% mutate(nkill_tot = nkill_270))
mod3 <- felm(rep_tp_share ~ nkill_tot | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 180) %>% mutate(nkill_tot = nkill_180))
mod4 <- felm(rep_tp_share ~ nkill_tot | fips + election_year | 0 | fips, data = pres_all %>% filter(days_till_election_last <= 90) %>% mutate(nkill_tot = nkill_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

#Table B9
pres_all_sub <- pres_all %>% filter(as.numeric(as.character(election_year)) <= 2000)

mod1 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all_sub)
mod2 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all_sub %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all_sub %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all_sub %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

#Table B10
pres_all_sub <- pres_all %>% filter(as.numeric(as.character(election_year)) > 2000)

mod1 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all_sub)
mod2 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all_sub %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all_sub %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = pres_all_sub %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

#Table B11
pres_all_sub <- pres_all %>% filter(as.numeric(as.character(election_year)) <= 2000)

mod1 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count | fips + election_year | 0 | fips, data = pres_all_sub)
mod2 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count | fips + election_year | 0 | fips, data = pres_all_sub, subset = (days_till_election_last <= 270))
mod3 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count | fips + election_year | 0 | fips, data = pres_all_sub, subset = (days_till_election_last <= 180))
mod4 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count | fips + election_year | 0 | fips, data = pres_all_sub, subset = (days_till_election_last <= 90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))


#Table B12

# repeat appendix_a_descriptive for subset

terror <- read.dta13("terror_pres_wide.dta") %>% mutate(motive2 = ifelse(motive2 == "Mormon", "Religious", motive2), motive2 = ifelse(motive2 == "", "Unknown", motive2), motive1 = ifelse(motive1 == "Religious", "Unknown", motive1), motive1 = ifelse(motive1 == "", "Unknown", motive1))

terror <- terror %>%
  filter(year <= 2000) %>%
  group_by(motive1, motive2) %>%
  mutate(temp = n())

terror$motive2[terror$temp < 2] <- "Other"

terror <- terror %>%
  select(-temp)

terror_motives <- terror %>% 
  select(year, success, motive1, motive2) %>%
  ungroup() %>%
  group_by(motive1) %>%
  mutate(cat_count = n()) %>%
  ungroup() %>%
  group_by(motive1, motive2) %>%
  mutate(count = n(), perc = count / cat_count, perc_success = (sum(success == 1) / count) * 100) %>%
  select(-c(year, success, cat_count)) %>%
  distinct()

terror_motives_1 <- terror %>%
  select(year, success, motive1, motive2) %>%
  group_by(motive1) %>% 
  summarize(motive2 = NA, count = n(), perc = count / 2639, perc_success = (sum(success == 1) / count) * 100)

terror_motives <- bind_rows(terror_motives, terror_motives_1) %>% arrange(motive1, desc(count))

print(xtable(terror_motives, type = latex), include.rownames = FALSE)

#Table B13
pres_all_sub <- pres_all %>% filter(as.numeric(as.character(election_year)) > 2000)

mod1 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count | fips + election_year | 0 | fips, data = pres_all_sub)
mod2 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count | fips + election_year | 0 | fips, data = pres_all_sub, subset = (days_till_election_last <= 270))
mod3 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count | fips + election_year | 0 | fips, data = pres_all_sub, subset = (days_till_election_last <= 180))
mod4 <- felm(rep_tp_share ~ hatred_count + abortion_count + political_count + unknown_count | fips + election_year | 0 | fips, data = pres_all_sub, subset = (days_till_election_last <= 90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

#Table B14

# repeat appendix_a_descriptive for subset

terror <- read.dta13("terror_pres_wide.dta") %>% mutate(motive2 = ifelse(motive2 == "Mormon", "Religious", motive2), motive2 = ifelse(motive2 == "", "Unknown", motive2), motive1 = ifelse(motive1 == "Religious", "Unknown", motive1), motive1 = ifelse(motive1 == "", "Unknown", motive1))

terror <- terror %>%
  filter(year > 2000) %>%
  group_by(motive1, motive2) %>%
  mutate(temp = n())

terror$motive2[terror$temp < 2] <- "Other"

terror <- terror %>%
  select(-temp)

terror_motives <- terror %>% 
  select(year, success, motive1, motive2) %>%
  ungroup() %>%
  group_by(motive1) %>%
  mutate(cat_count = n()) %>%
  ungroup() %>%
  group_by(motive1, motive2) %>%
  mutate(count = n(), perc = count / cat_count, perc_success = (sum(success == 1) / count) * 100) %>%
  select(-c(year, success, cat_count)) %>%
  distinct()

terror_motives_1 <- terror %>%
  select(year, success, motive1, motive2) %>%
  group_by(motive1) %>% 
  summarize(motive2 = NA, count = n(), perc = count / 2639, perc_success = (sum(success == 1) / count) * 100)

terror_motives <- bind_rows(terror_motives, terror_motives_1) %>% arrange(motive1, desc(count))

print(xtable(terror_motives, type = latex), include.rownames = FALSE)

#Table B15

mod1 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = house_all)
mod2 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = house_all %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = house_all %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = house_all %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

# Table B16

mod1 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = senate_all)
mod2 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = senate_all %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = senate_all %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count | fips + election_year | 0 | fips, data = senate_all %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

# Table B17

mod1 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = house_all)
mod2 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = house_all %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = house_all %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = house_all %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))

#Table B18

mod1 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = senate_all)
mod2 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = senate_all %>% filter(days_till_election_last <= 270) %>% mutate(success_count = success_270))
mod3 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = senate_all %>% filter(days_till_election_last <= 180) %>% mutate(success_count = success_180))
mod4 <- felm(rep_tp_share ~ success_count + l_retirement_per_cap + l_unemp_insurance_per_cap + l_murder_per_cap | fips + election_year | 0 | fips, data = senate_all %>% filter(days_till_election_last <= 90) %>% mutate(success_count = success_90))

stargazer(mod1, mod2, mod3, mod4, type = "latex", dep.var.labels = "Republican Two-Party Vote Share", dep.var.caption = "", title = "Year and County FEs, County Clustered SEs",
          column.labels = c("All Attacks","9 months","6 months","3 months"))


